package com.gis507.admin.controller;

import com.gis507.admin.bean.User;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;

/**
 * @author LnnuUser
 * @create 2021-07-04-下午2:49
 */
@Controller
public class IndexController {

    /**
     * 发请求来到登录页面
     * @return
     */
    //以下两种写法都是响应到登录页
//    @GetMapping(value = "/")
    @GetMapping(value = {"/","/login"})
    public String loginPage(){

        return "login";
    }

    @PostMapping("/login")
    public String main(User user, HttpSession session, Model model){

        if (StringUtils.hasLength(user.getUserName()) && StringUtils.hasLength(user.getPassword())){
            //把登录成功的用户保存起来
            session.setAttribute("loginUser",user);
            //登陆成功重定向到main.html页面；重定向防止表单重复提交
            return "redirect:/main.html";
        }else{
            model.addAttribute("msg","账号密码错误");
            //回到登录页
            return "login";
        }


    }

    /**
     * 真正的去main页面
     * @return
     */
    @GetMapping("/main.html")
    public String mainpage(HttpSession session, Model model){

        //是否登录，拦截器，过滤器
//        Object loginUser = session.getAttribute("loginUser");
//        if (loginUser != null){
//            return "main";
//        }else{
//            model.addAttribute("msg","当前未登录");
//            return "login";
//        }
        return "main";
    }
}
